package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

 @Reference
 private ExtCproductService extCproductService;

 @Reference
 private FactoryService factoryService;

 /**
  * 分页展示
  * /cargo/extCproduct/list.do?contractId=24f300f1-eaf1-40d1-aae9-5297b7ef842c&contractProductId=7ac27daa-d850-4a55-b227-701cd1076d3d
  * @return
  */
 @RequestMapping("/list")
 public String list(
  @RequestParam(defaultValue = "1")int page,
  @RequestParam(defaultValue = "5") int size,
  String contractId,String contractProductId
 ) {
  ExtCproductExample example = new ExtCproductExample();
  ExtCproductExample.Criteria criteria = example.createCriteria();
  criteria.andContractProductIdEqualTo(contractProductId);
  PageInfo info = extCproductService.findAll(example, page, size);
  request.setAttribute("page",info);
  //查询所有附件厂家
  FactoryExample factoryExample = new FactoryExample();
  FactoryExample.Criteria factoryCriteria = factoryExample.createCriteria();
  factoryCriteria.andCtypeEqualTo("附件");
  List<Factory> list = factoryService.findAll(factoryExample);
  request.setAttribute("factoryList", list);
  //为了保存方便，需要将货物id和购销合同id存入request域，在页面展示
  request.setAttribute("contractId", contractId);
  request.setAttribute("contractProductId",contractProductId);
  //跳转页面
  return "cargo/extc/extc-list";
 }

 /**
  * 保存或更新
  */
 @RequestMapping("/edit")
 public String edit(ExtCproduct extc) {
  //设置企业属性
  extc.setCompanyId(getLoginCompanyId());
  extc.setCompanyName(getLoginCompanyName());
  //判断是否具有id
  if (StringUtils.isEmpty(extc.getId())) {
   //没有id，保存
   extCproductService.save(extc);
  } else {
   //有id，更新
   extCproductService.update(extc);
  }
  //重定向
  return "redirect:/cargo/extCproduct/list.do?contractId=" + extc.getContractId() + "&contractProductId=" + extc.getContractProductId();
 }

 /**
  * 进入修改页面
  *
  */
 @RequestMapping("/toUpdate")
 public String toUpdate(String id) {
  //根据id查询
  ExtCproduct extCproduct = extCproductService.findById(id);
  request.setAttribute("extCproduct",extCproduct);
  //查询生产厂家
  FactoryExample factoryExample = new FactoryExample();
  FactoryExample.Criteria criteria = factoryExample.createCriteria();
  criteria.andCtypeEqualTo("附件");
  List<Factory> list = factoryService.findAll(factoryExample);
  request.setAttribute("factoryList", list);
  //跳转页面
  return "cargo/extc/extc-update";
 }

 /**
  * 删除附件
  */
 @RequestMapping("/delete")
 public String delete(String id,String contractId,String contractProductId) {
  //业务逻辑
  extCproductService.delete(id);
  //重定向
  return  "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
 }
}
